<?php 
// 标签管理控制器
class TagController extends AuthController{
	private $model;
	public function __init(){
		$this->model = K('Tag');
	}

	public function index(){
		// 统计条数
		$total = $this->model->count();
		$pageObj = new Page($total,4,2);
		// 显示分页
		$page = $pageObj->show();
		$this->assign('page',$page);
		// 算出要limit的位置
		$limit = $pageObj->limit();
		// 每次点击分页截取不同的数据

		$data = $this->model->limit($limit)->all();
		$this->assign('data',$data);
		$this->display();
	}

	public function add(){
		if(IS_POST){
			header('Content-type:text/html;charset=utf-8');
			$tagName = Q('post.tagname');
			// 把字符串变成数组
			$tagArr = explode("\r", $tagName);
			// 调用模型添加
			if(!$this->model->addData($tagArr)){
				$this->error($this->model->error);
			}
			$this->success('添加成功');

		}
		$this->display();
	}

	public function edit(){
		$tid = Q('get.tid',0,'intval');
		if(IS_POST){
			if(!$this->model->edit($tid)){
				$this->error($this->model->error);
			}
			$this->success('编辑成功',U('index'));
		}
		$oldData  = $this->model->where("tid={$tid}")->find();
		$this->assign('oldData',$oldData);
		$this->display();
	}

	public function del(){
		$tid = Q('get.tid',0,'intval');
		$this->model->where("tid={$tid}")->delete();
		$this->success('删除成功');
	}
}


 ?>